package com.example.ivideo_2211a.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.example.ivideo_2211a.R
import com.example.ivideo_2211a.databinding.ItemDanmuBinding
import com.example.ivideo_2211a.model.DanmuMessage

/**
 * 弹幕列表适配器
 */
class DanmuAdapter : RecyclerView.Adapter<DanmuAdapter.DanmuViewHolder>() {
    
    private val danmuList = mutableListOf<DanmuMessage>()
    private val maxDanmuCount = 100 // 最大弹幕数量，防止内存溢出
    
    /**
     * 添加单条弹幕
     */
    fun addDanmu(danmu: DanmuMessage) {
        // 如果超过最大数量，移除最旧的弹幕
        if (danmuList.size >= maxDanmuCount) {
            danmuList.removeAt(0)
            notifyItemRemoved(0)
        }
        
        danmuList.add(danmu)
        notifyItemInserted(danmuList.size - 1)
    }
    
    /**
     * 批量添加弹幕
     */
    fun addDanmuList(danmus: List<DanmuMessage>) {
        val startPosition = danmuList.size
        
        // 如果添加后超过最大数量，先移除旧的弹幕
        val totalCount = danmuList.size + danmus.size
        if (totalCount > maxDanmuCount) {
            val removeCount = totalCount - maxDanmuCount
            repeat(removeCount) {
                if (danmuList.isNotEmpty()) {
                    danmuList.removeAt(0)
                }
            }
            notifyItemRangeRemoved(0, removeCount)
        }
        
        danmuList.addAll(danmus)
        notifyItemRangeInserted(startPosition, danmus.size)
    }
    
    /**
     * 清空弹幕列表
     */
    fun clearDanmu() {
        val size = danmuList.size
        danmuList.clear()
        notifyItemRangeRemoved(0, size)
    }
    
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DanmuViewHolder {
        val binding = ItemDanmuBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
        return DanmuViewHolder(binding)
    }
    
    override fun onBindViewHolder(holder: DanmuViewHolder, position: Int) {
        holder.bind(danmuList[position])
    }
    
    override fun getItemCount(): Int = danmuList.size
    
    /**
     * 弹幕ViewHolder
     */
    class DanmuViewHolder(private val binding: ItemDanmuBinding) : RecyclerView.ViewHolder(binding.root) {
        
        fun bind(danmu: DanmuMessage) {
            with(binding) {
                // 设置用户名
                tvUsername.text = danmu.username
                
                // 设置消息内容
                tvMessage.text = danmu.message
                
                // 设置时间
                tvTime.text = danmu.getFormattedTime()
                
                // 根据消息类型设置不同的样式
                when (danmu.messageType) {
                    DanmuMessage.MessageType.NORMAL -> {
                        tvUsername.setTextColor(ContextCompat.getColor(root.context, R.color.white))
                        tvMessage.setTextColor(ContextCompat.getColor(root.context, R.color.white))
                        root.setBackgroundResource(R.drawable.bg_danmu_normal)
                    }
                    DanmuMessage.MessageType.GIFT -> {
                        tvUsername.setTextColor(ContextCompat.getColor(root.context, R.color.orange))
                        tvMessage.setTextColor(ContextCompat.getColor(root.context, R.color.orange))
                        root.setBackgroundResource(R.drawable.bg_danmu_gift)
                    }
                    DanmuMessage.MessageType.SYSTEM -> {
                        tvUsername.setTextColor(ContextCompat.getColor(root.context, R.color.yellow))
                        tvMessage.setTextColor(ContextCompat.getColor(root.context, R.color.yellow))
                        root.setBackgroundResource(R.drawable.bg_danmu_system)
                    }
                }
            }
        }
    }
}